package com.zztl.infiniteBand.ui.activity

import android.graphics.BitmapFactory
import android.text.TextUtils
import android.view.View
import butterknife.OnClick
import com.uuzuche.lib_zxing.activity.CodeUtils
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.eventbus.RefreshAssetsEvent
import com.zztl.infiniteBand.model.AddressBean
import com.zztl.infiniteBand.net.requestBean.GetWalletAddrRequest
import com.zztl.infiniteBand.presenter.IncomePresenter
import com.zztl.infiniteBand.ui.view.mvpview.IncomeView
import kotlinx.android.synthetic.main.activity_recharge.*
import org.greenrobot.eventbus.EventBus

class RechargeActivity : BaseMvpActivity<IncomeView,IncomePresenter>(), IncomeView {

   /* @BindView(R.id.iv_addr_qr)
     lateinit var iv_addr_qr: ImageView
    @BindView(R.id.tv_addr)
    lateinit var tv_addr: TextView*/
    lateinit var mIncomePresenter: IncomePresenter

    override fun setLayoutId(): Int =R.layout.activity_recharge


    override fun initBase() {
        setTitle(getString(R.string.充值))
        mIncomePresenter.getAddress(GetWalletAddrRequest("InfiniteBand000001"), this)
    }


    override fun createPresenter(): IncomePresenter {
        mIncomePresenter = IncomePresenter()
        return mIncomePresenter
    }


    @OnClick(R.id.btn_copy)
    fun click(view: View) {
        when (view.id) {
            R.id.btn_copy -> {
                val address = tv_addr.text.toString().trim { it <= ' ' }
                mIncomePresenter.copyAddress(address, this)
            }
            else -> {
            }
        }
    }

    override fun getAddrSuccess(bean: AddressBean) {
        val address = bean.data!!.address
        if (!TextUtils.isEmpty(address)) {
            tv_addr.text = address
            val bitmap = CodeUtils.createImage(address, iv_addr_qr.measuredWidth, iv_addr_qr.measuredHeight, BitmapFactory.decodeResource(resources, R.mipmap.logo_oval))
            iv_addr_qr.setImageBitmap(bitmap)
        }
    }


    override fun onDestroy() {
        EventBus.getDefault().post(RefreshAssetsEvent())
        super.onDestroy()
    }
}
